package org.example.softwarearchitecture.service;

import lombok.RequiredArgsConstructor;
import org.example.softwarearchitecture.dox.Address;
import org.example.softwarearchitecture.dox.User;
import org.example.softwarearchitecture.repository.AddressRepository;
import org.example.softwarearchitecture.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserService {

    private final UserRepository userRepository;
    private final AddressRepository addressRepository;

    @Transactional
    public User addUser(User user) {
        return userRepository.save(user);
    }

    @Transactional
    public User getUser(int id){
        return userRepository.findById(id);
    }

    @Transactional
    public Address addAddress(Address address) {
        return addressRepository.save(address);
    }

    @Transactional
    public User getUser(String account,String password){
        return userRepository.find(account,password);
    }

    @Transactional
    public List<Address> listAddresses(int uid){
        return addressRepository.findAddresses(uid);
    }
}
